package org.ns.summer.classpath.scan.io;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.ns.summer.classpath.scan.ScanFile;

public class ScanFileInputStream extends InputStream{
	InputStream inputStream = null;
	Map<String, ScanFileIOStreamParse> suffixIOParses = new HashMap<String, ScanFileIOStreamParse>();
	ScanFileIOStreamParse defaultIOParse = new DefaultScanFileIOStreamParse();
	public ScanFileInputStream(String absolutePath) throws IOException {
		this(new ScanFile(absolutePath));
	}
	public ScanFileInputStream(ScanFile scanFile) throws IOException {
		suffixIOParses.put("jar", new JarScanFileIOStreamParse());
		String suffixName =  scanFile.getRootSuffix();
		ScanFileIOStreamParse scanFileIOStreamParse = suffixIOParses.get(suffixName);
		if(scanFileIOStreamParse == null){
			scanFileIOStreamParse = defaultIOParse;
		}
		inputStream = scanFileIOStreamParse.getInputStream(scanFile);
	}
	@Override
	public int read() throws IOException {
		
		return inputStream.read();
	}
	@Override
	public int available() throws IOException {
		return inputStream.available();
	}
	@Override
	public void close() throws IOException {
		inputStream.close();
	}
	@Override
	public synchronized void mark(int readlimit) {
		inputStream.mark(readlimit);
	}
	@Override
	public synchronized void reset() throws IOException {
		inputStream.reset();
	}
	@Override
	public boolean markSupported() {
		return inputStream.markSupported();
	}
	
}
